package com.uum.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uum.admin.mapper.SysOperationLogMapper;
import com.uum.admin.pojo.query.OperationPageQuery;
import com.uum.admin.pojo.vo.OperationVO;
import com.uum.admin.service.SysOperationLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uum.common.core.entity.SysOperationLog;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogMapper, SysOperationLog> implements SysOperationLogService {

    @Override
    public IPage<OperationVO> page(OperationPageQuery query) {
        IPage<OperationVO> page = new Page(query.getCurrentPage(), query.getPageSize());
        String username = query.getUsername();
        String microService = query.getMicroService();
        String methodClass = query.getMethodClass();
        Integer businessType = query.getBusinessType();
        String status = query.getStatus();
        String requestMethod = query.getRequestMethod();
        String startTime = query.getStartTime();
        String endTime = query.getEndTime();

        QueryWrapper wrapper = new QueryWrapper<>();

        if (StrUtil.isNotBlank(username)) {
            wrapper.eq("t2.username", username);
        }
        if (StrUtil.isNotBlank(microService)) {
            wrapper.eq("t1.micro_service", microService);
        }
        if (StrUtil.isNotBlank(methodClass)) {
            wrapper.like("t1.method_class", methodClass);
        }
        if (businessType != null) {
            wrapper.eq("t1.business_type", businessType);
        }
        if (StrUtil.isNotBlank(status)) {
            wrapper.eq("t1.status", status);
        }
        if (StrUtil.isNotBlank(requestMethod)) {
            wrapper.eq("t1.request_method", requestMethod);
        }
        if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
            wrapper.between("t1.operation_time", startTime, endTime);
        }
        wrapper.orderByDesc("t1.operation_time");
        List<OperationVO> list = baseMapper.page(page, wrapper);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<Map<String, Object>> getPVByWeek(String startTime,String endTime) {
        return baseMapper.getPVByWeek(startTime,endTime);
    }

}
